<template>
  <div>
    <div class="headerOperation">
      <div style="float:left;margin-left:10px;">
        <el-input v-model="searchKey" placeholder="请输入你要查找的内容" style="width:200px;margin-right:10px;" />
        <el-button type="primary" @click="handleFilter">过滤</el-button>
      </div>
      <div style="float:right;margin-right:10px;">
        <el-button type="primary" @click="handleAdd">添加</el-button>
        <el-button type="primary" @click="handleRefresh">刷新</el-button>
      </div>
    </div>
    <div>
      <el-table :data="productList">
        <el-table-column label="Id" prop="id" />
        <el-table-column label="商品名称" prop="productName" />
        <el-table-column label="价格" prop="price" />
        <el-table-column align="center" label="操作">
          <template slot-scope="{row,$index}">
            <el-button type="primary" @click="handleEdit(row,$index)">编辑</el-button>
            <el-button type="danger" @click="handleDel($index)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-dialog title="收货地址" :visible.sync="dialogFormVisible">
        <el-form ref="formData" :model="form">
          <el-form-item label="id" prop="id" :label-width="lableWidth">
            <el-input v-model="form.id" autocomplete="off" class="inputWidth" />
          </el-form-item>
          <el-form-item label="商品名称" prop="productName" :label-width="lableWidth">
            <el-input v-model="form.productName" autocomplete="off" class="inputWidth" />
          </el-form-item>
          <el-form-item label="价格" prop="price" :label-width="lableWidth">
            <el-input v-model="form.price" autocomplete="off" class="inputWidth" />
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button @click="handleFormCancel">取 消</el-button>
          <el-button type="primary" @click="handleFormConfirm">确 定</el-button>
        </div>
      </el-dialog>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      productList: [],
      searchKey: '',
      dialogFormVisible: false,
      isAddBtnClick: true,
      lableWidth: '20%',
      currentIndex: -1,
      form: {
        id: 0,
        productName: '',
        price: 0
      }
    }
  },
  created: function() {
    this.productList = this.fetchList()
  },
  methods: {
    fetchList: function() {
      return [
        {
          id: 1,
          productName: 'Iphone 7',
          price: 5889
        },
        {
          id: 2,
          productName: 'Iphone 8 Plus',
          price: 7889
        },
        {
          id: 3,
          productName: 'Iphone 11 Pro',
          price: 9899
        },
        {
          id: 4,
          productName: '三星 Note20 Ultra',
          price: 8999
        }
      ]
    },
    handleFilter: function() {
      console.log(this.searchKey)
      var originList = this.fetchList()
      var list = originList.filter(item => item.productName.indexOf(this.searchKey) >= 0)
      this.productList = list
    },
    handleAdd: function() {
      // this.$refs.formData.resetFields()
      // this['formData'] = {}
      // this.$nextTick(() => {
      //   console.log(this.$refs['formData'])
      //   this.$refs['formData'].resetFields()
      // })
      this.form.id = 0
      this.form.productName = ''
      this.form.price = 0
      this.dialogFormVisible = true
      this.isAddBtnClick = true
    },
    handleRefresh: function() {
      this.productList = this.fetchList()
    },
    handleEdit: function(item, index) {
      this.currentIndex = index
      console.log(item)
      this.form = Object.assign({}, item)
      this.dialogFormVisible = true
      this.isAddBtnClick = false
    },
    handleDel: function(index) {
      this.$confirm('此操作将永久删除该记录, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.productList.splice(index, 1)
        this.$message({
          type: 'success',
          message: '删除成功!'
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
      console.log(index)
    },
    handleFormCancel: function() {
      this.dialogFormVisible = false
    },
    handleFormConfirm: function() {
      this.dialogFormVisible = false
      var temp = Object.assign({}, this.form)
      if (this.isAddBtnClick) {
        this.productList.push(temp)
      } else {
        this.productList.splice(this.currentIndex, 1, temp)
      }
    }
  }
}
</script>
<style scoped>
  .headerOperation{
    margin-top :10px;
  }
  .inputWidth{
    width: 85%
  }
</style>
